/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.dialog.base;

import android.content.Context;

import androidx.annotation.NonNull;

import com.ecarx.systemui.plugin.common.CommonUtils;
import com.ecarx.systemui.plugin.controller.TcamWifiController;
import com.ecarx.systemui.plugin.ui.dialog.BTWifiDialog;
import com.ecarx.systemui.plugin.ui.dialog.CarLocationDialog;
import com.ecarx.systemui.plugin.ui.dialog.DriverSeatWheelDialog;
import com.ecarx.systemui.plugin.ui.dialog.DriverTempDialog;
import com.ecarx.systemui.plugin.ui.dialog.ErrorInfoDialog;
import com.ecarx.systemui.plugin.ui.dialog.FanControlDialog;
import com.ecarx.systemui.plugin.ui.dialog.GPSDialog;
import com.ecarx.systemui.plugin.ui.dialog.GlobalVolumeDialog;
import com.ecarx.systemui.plugin.ui.dialog.InputTextDialog;
import com.ecarx.systemui.plugin.ui.dialog.PassengerSeatDialog;
import com.ecarx.systemui.plugin.ui.dialog.PassengerTempDialog;
import com.ecarx.systemui.plugin.ui.dialog.UsbDialog;
import com.ecarx.systemui.plugin.ui.dialog.VolumeDialog;
import com.ecarx.systemui.plugin.ui.dialog.calendar.CalendarTimeDialog;
import com.ecarx.systemui.plugin.utils.LogUtils;
import com.ecarx.systemui.plugin.ui.dialog.WpcDialog;

public class DialogManager {

    private static final String TAG = "DialogManager";

    public static final int TYPE_VOLUME_OVERLAY = android.view.WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 20;

    private DialogManager() {

    }

    private static class Holder {
        private static final DialogManager INSTANCE = new DialogManager();
    }

    public static DialogManager getDialogManager() {
        return Holder.INSTANCE;
    }

    private FanControlDialog fanControlDialog;

    private DriverSeatWheelDialog driverSeatWheelDialog;
    private PassengerSeatDialog passengerSeatDialog;
    private DriverTempDialog driverTempDialog;
    private PassengerTempDialog passengerTempDialog;
    private BTWifiDialog btWifiDialog;
    private UsbDialog usbDialog;
    private VolumeDialog volumeDialog;
    private GlobalVolumeDialog globalVolumeDialog;
    private CalendarTimeDialog mCalendarTimeDialog;
    private GPSDialog mGPSDialog;
    private WpcDialog mWpcDialog;
    private CarLocationDialog mCarLocationDialog;


    public void disMissOtherDialog(AutoCloseDialog selfDialog) {
        disMissSomeOneDialog(fanControlDialog, selfDialog instanceof FanControlDialog);
        disMissSomeOneDialog(driverSeatWheelDialog, selfDialog instanceof DriverSeatWheelDialog);
        disMissSomeOneDialog(passengerSeatDialog, selfDialog instanceof PassengerSeatDialog);
        disMissSomeOneDialog(driverTempDialog, selfDialog instanceof DriverTempDialog);
        disMissSomeOneDialog(passengerTempDialog, selfDialog instanceof PassengerTempDialog);
        disMissSomeOneDialog(btWifiDialog, selfDialog instanceof BTWifiDialog);
        disMissSomeOneDialog(usbDialog, selfDialog instanceof UsbDialog);
        disMissSomeOneDialog(volumeDialog, selfDialog instanceof VolumeDialog);
        disMissSomeOneDialog(globalVolumeDialog, selfDialog instanceof VolumeDialog);

        disMissSomeOneDialog(mWpcDialog, selfDialog instanceof WpcDialog);
        disMissSomeOneDialog(mCarLocationDialog, selfDialog instanceof CarLocationDialog);
    }

    public void disMissAllDialog() {
        disMissSomeOneDialog(fanControlDialog);
        disMissSomeOneDialog(driverSeatWheelDialog);
        disMissSomeOneDialog(passengerSeatDialog);
        disMissSomeOneDialog(driverTempDialog);
        disMissSomeOneDialog(passengerTempDialog);
        disMissSomeOneDialog(btWifiDialog);
        disMissSomeOneDialog(usbDialog);
        disMissSomeOneDialog(mWpcDialog);
        disMissSomeOneDialog(mCarLocationDialog);
    }

    private void disMissSomeOneDialog(AutoCloseDialog innerDialog, boolean isMine) {
        if (innerDialog != null && innerDialog.isShowing() && !isMine) {
            innerDialog.delayedDismiss();
        }
    }

    private void disMissSomeOneDialog(AutoCloseDialog innerDialog) {
        if (innerDialog != null && innerDialog.isShowing()) {
            innerDialog.delayedDismiss();
        }
    }


    public FanControlDialog showFanControlDialog(Context context) {
        if (fanControlDialog == null) {
            fanControlDialog = new FanControlDialog(context);
        }
        fanControlDialog.show();
        disMissOtherDialog(fanControlDialog);
        return fanControlDialog;
    }

    public DriverSeatWheelDialog showDriverSeatWheelDialog(Context context, int[] location, boolean isShowWheel,
                                                           boolean isShowSeatHeat, boolean isShowSeatVent) {
        if (driverSeatWheelDialog == null) {
            driverSeatWheelDialog = new DriverSeatWheelDialog(location, context);
            driverSeatWheelDialog.show();
            driverSeatWheelDialog.updateShowIcon(isShowWheel, isShowSeatHeat, isShowSeatVent);
        } else {
            driverSeatWheelDialog.updateConfig(location);
            if (driverSeatWheelDialog.isShowing()) {
                driverSeatWheelDialog.updateAgainShowIcon(isShowWheel, isShowSeatHeat, isShowSeatVent);
            } else {
                driverSeatWheelDialog.show();
                driverSeatWheelDialog.updateShowIcon(isShowWheel, isShowSeatHeat, isShowSeatVent);
            }
        }
        disMissOtherDialog(driverSeatWheelDialog);
        return driverSeatWheelDialog;
    }


    public PassengerSeatDialog showPassengerSeatDialog(Context context, int[] location) {
        if (passengerSeatDialog == null) {
            passengerSeatDialog = new PassengerSeatDialog(location, context);
            passengerSeatDialog.show();
        } else {
            passengerSeatDialog.updateConfig(location);
            if (passengerSeatDialog.isShowing()) {
                passengerSeatDialog.updateAgainShow();
            } else {
                passengerSeatDialog.show();
            }
        }
        disMissOtherDialog(passengerSeatDialog);
        return passengerSeatDialog;
    }


    public void showTempDialog(int[] location, Context context, boolean isCsd) {

        if (isCsd) {
            LogUtils.d(TAG, "driverTempDialog:" + driverTempDialog + "  this:" + this);
            if (driverTempDialog == null) {
                driverTempDialog = new DriverTempDialog(location, CommonUtils.getDisplayContext(context, isCsd));
                driverTempDialog.show();
            } else {
                if (driverTempDialog.isShowing()) {
                } else {
                    driverTempDialog.show();
                }
            }
//            disMissOtherDialog(driverTempDialog);
        } else {
            if (passengerTempDialog == null) {
                passengerTempDialog = new PassengerTempDialog(location, CommonUtils.getDisplayContext(context, isCsd));
                passengerTempDialog.show();
            } else {
                if (passengerTempDialog.isShowing()) {
                } else {
                    passengerTempDialog.show();
                }
            }
//            disMissOtherDialog(passengerTempDialog);
        }
    }

    public void showBtWifiDialog(Context context, int[] location, int btOrWifi){
        showBtWifiDialog(context, location, btOrWifi, true);
    }

    public void showBtWifiDialog(Context context, int[] location, int btOrWifi, boolean isCsd) {
        if (btWifiDialog == null) {
            btWifiDialog = new BTWifiDialog(CommonUtils.getDisplayContext(context, isCsd), location);
            btWifiDialog.show();
            btWifiDialog.showPage(btOrWifi);
        } else {
            btWifiDialog.updateConfig(location);
            if (btWifiDialog.isShowing()) {
                btWifiDialog.showDialogAgain(btOrWifi);
            } else {
                btWifiDialog.show();
                btWifiDialog.showPage(btOrWifi);
            }
        }
        disMissOtherDialog(btWifiDialog);
    }

    public void dismissBtWifiDialog(){
        if (btWifiDialog != null) {
            btWifiDialog.dismiss();
        }
    }

    public void showUsbDialog(Context context, int[] location){
        showUsbDialog(context, location, true);
    }

    public void showUsbDialog(Context context, int[] location, boolean isCsd) {
        if (usbDialog == null) {
            usbDialog = new UsbDialog(CommonUtils.getDisplayContext(context, isCsd), location);
            usbDialog.show();
        } else {
            usbDialog.updateConfig(location);
            if (usbDialog.isShowing()) {
                usbDialog.showDialogAgain();
            } else {
                usbDialog.show();
            }
        }
        disMissOtherDialog(usbDialog);
    }

    public DriverTempDialog getDriverTempDialog() {
        return driverTempDialog;
    }

    public PassengerTempDialog getPassengerTempDialog() {
        return passengerTempDialog;
    }

    public void cleanDriverTempDialog() {
        driverTempDialog = null;
    }

    public void cleanPassengerTempDialog() {
        passengerTempDialog = null;
    }

    public void cleanDriverSeatWheelDialog() {
        driverSeatWheelDialog = null;
    }

    public void cleanPassengerSeatDialog() {
        passengerSeatDialog = null;
    }

    public void cleanBTWifiDialog() {
        btWifiDialog = null;
    }

    public void cleanUsbDialog() {
        usbDialog = null;
    }

    public UsbDialog getUsbDialog() {
        return usbDialog;
    }

    public VolumeDialog getVolumeDialog() {
        return volumeDialog;
    }

    public void showVolumeDialog(Context context, boolean isCSD) {
        if (volumeDialog == null) {
            volumeDialog = new VolumeDialog(CommonUtils.getDisplayContext(context, isCSD));
            volumeDialog.show();
        } else {
            if (!volumeDialog.isShowing()) {
                volumeDialog.show();
            }
        }
        disMissOtherDialog(volumeDialog);

    }

    public void cleanVolumeDialog() {
        volumeDialog = null;
    }

    public void showGlobalVolumeDialog(Context context, boolean isCSD) {
        if (globalVolumeDialog == null) {
            globalVolumeDialog = new GlobalVolumeDialog(CommonUtils.getDisplayContext(context, isCSD));
            globalVolumeDialog.show();
        } else {
            if (!globalVolumeDialog.isShowing()) {
                globalVolumeDialog.show();
            }
        }
        disMissOtherDialog(globalVolumeDialog);

    }

    public void cleanGlobalVolumeDialog() {
        globalVolumeDialog = null;
    }


    private GPSDialog createGPSDialog(Context context) {
        return new GPSDialog(context);
    }


    private CalendarTimeDialog createCalendarTimeDialog(Context context) {
        return new CalendarTimeDialog(context);
    }

    public void showCalendarTimeDialog(Context context, boolean isCsd) {
        LogUtils.d(TAG, "showCalendarTimeDialog: " + (mCalendarTimeDialog == null));
        if (mCalendarTimeDialog == null) {
            mCalendarTimeDialog = createCalendarTimeDialog(CommonUtils.getDisplayContext(context, isCsd));
        }
        mCalendarTimeDialog.setOnDismissListener(dialog -> {
            mCalendarTimeDialog = null;
        });
        mCalendarTimeDialog.setCanceledOnTouchOutside(true);
        LogUtils.d(TAG, "showCalendarTimeDialog:" + mCalendarTimeDialog.isShowing());
        if (!mCalendarTimeDialog.isShowing()) {
            mCalendarTimeDialog.show();
        }
    }

    public void showGPSDialog(Context context, boolean isCsd) {
        if (mGPSDialog == null) {
            mGPSDialog = createGPSDialog(CommonUtils.getDisplayContext(context, isCsd));
        }
        mGPSDialog.setOnDismissListener(dialog -> {
            mGPSDialog = null;
        });
        LogUtils.d(TAG, "showGPSDialog:" + mGPSDialog.isShowing());
        if (!mGPSDialog.isShowing()) {
            mGPSDialog.show();
        }
    }





    public WpcDialog getWpcDialog() {
        return mWpcDialog;
    }

    public CarLocationDialog getCarLocationDialog() {
        return mCarLocationDialog;
    }

    /**
     * 无线充电
     * @param context
     * @param location
     * @param status
     */
    public void showWpcDialog(Context context, int location, int status) {
        if (mWpcDialog == null) {
            mWpcDialog = new WpcDialog(location, CommonUtils.getDisplayContext(context, true));
            mWpcDialog.show(status);
        } else {
            mWpcDialog.updateConfig(location);
            if (mWpcDialog.isShowing()) {
                mWpcDialog.update(status);
            } else {
                mWpcDialog.show(status);
            }
        }
        disMissOtherDialog(mWpcDialog);
    }

    /**
     * 车辆位置
     * @param context
     * @param location
     */
    public void showCarLocationDialog(Context context, int location) {
        if (mCarLocationDialog == null) {
            mCarLocationDialog = new CarLocationDialog(location, CommonUtils.getDisplayContext(context, true));
            mCarLocationDialog.show();
        } else {
            mCarLocationDialog.updateConfig(location);
            if (mCarLocationDialog.isShowing()) {
            } else {
                mCarLocationDialog.show();
            }
        }
        disMissOtherDialog(mCarLocationDialog);
    }


    public static void showInputDialog(Context context, boolean isPwd, InputTextDialog.InputTextListener inputTextListener){
        showInputDialog(context, isPwd, true, inputTextListener);
    }

    public static void showInputDialog(Context context, boolean isPwd, boolean isCsd, InputTextDialog.InputTextListener inputTextListener){
        new InputTextDialog(CommonUtils.getDisplayContext(context, isCsd), isPwd, inputTextListener).show();
    }

    public static void showErrorInfoDialog(Context context, int titleId, int descId){
        showErrorInfoDialog(context, titleId, descId, true);
    }

    public static void showErrorInfoDialog(Context context, int titleId, int descId, boolean isCsd){
        new ErrorInfoDialog(CommonUtils.getDisplayContext(context, isCsd), context.getResources().getString(titleId), context.getResources().getString(descId)).show();
    }
}
